import json

from api.tests.base_test_case import BaseTestCase
from app.models import Category, Product, ProductSpec, MailingAddress, Cart


class PreOrderTest(BaseTestCase):
    def setUp(self):
        super(PreOrderTest, self).setUp()

        category = Category(name=u'豆叶类', description='XXXXX')
        category.save()
        product = Product(category=category, name=u'土豆', description=u'XXXXX')
        product.save()
        self.spec1 = ProductSpec(product=product, name=u'大土豆', unit=u'斤', price=1, stock=10, is_on_shell=True)
        self.spec1.save()

        self.spec2 = ProductSpec(product=product, name=u'小土豆', unit=u'斤', price=1, stock=10, is_on_shell=True)
        self.spec2.save()

        self.address = MailingAddress.objects.create(user=self.user, address="XXX楼XX市", addressee="ZXC",
                                                     phone="15652536271")
        self.address.save()

        self.cart1 = Cart.objects.create(user=self.user, spec=self.spec1, amount=1)
        self.cart1.save()

        self.cart2 = Cart.objects.create(user=self.user, spec=self.spec2, amount=2)
        self.cart2.save()

    def test_post_order(self):
        data = {
            "carts": [
                {
                    "cart_id": self.cart1.id,
                },
                {
                    "cart_id": self.cart2.id,
                },
            ]
        }

        response = self.client.post('/api/pre-order/',
                                    data=json.dumps(data),
                                    content_type='application/json',
                                    accept_media_type='application/json',
                                    HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(response.status_code, 200)
        # data = JSONParser().parse(BytesIO(response.content))
        # self.assertEqual(data['pre_order_id'], 1)
        # self.assertEquals(data['order_id'], 1)
        # order_id = data['order_id']
        # order = Order.objects.get(id=order_id)
